<?php
class Admin_ProductsController extends Zend_Controller_Action
{	
    public function init() {
        if (Zend_Auth::getInstance ( 'admin' )->setStorage ( new Zend_Auth_Storage_Session ( 'admin' ) )->hasIdentity () == false) {
			$this->_helper->getHelper ( 'Redirector' )->gotoUrl ( "admin/login" );
		} 
    }
    public function indexAction()
    {
      $this->view->title = "Products";	
    	try {
            $model = new Vina_Model_Products();
            $dataList = $model->fetchListToArray('1=1', 'id desc');
            $paginator         = Zend_Paginator::factory($dataList);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(10);
            $paginator->setPageRange(10);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
           
            $this->view->data = $paginator;     
                
         }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    
    }
	public function listProductsAction()
    {
      $this->view->title = "List product";	
	  $id                     =  $this->_request->getParam('id',false);	
    	try {
            $products = new Vina_Model_Products();
            $dataList = $products->fetchListToArray("sub2_category_id = $id ", 'id desc');
            $paginator         = Zend_Paginator::factory($dataList);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(10);
            $paginator->setPageRange(10);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
           
            $this->view->data = $paginator;
			$this->view->id = $id; 				
                
         }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    
    }
  public function addAction() {
      
        try {
           $model = new Vina_Model_Products();
           $form                   = new Admin_Form_Product();
          
           if ($this->_request->isPost()) {
                $formData = $this->_request->getPost();
                $formData = $this->_clean($formData);
                if ($form->isValid($formData)) {
                   $dataInsert = $form->getValues();
                   $model->setName($dataInsert['name']);
                   $model->setDescription($dataInsert['description']);
                   $model->setContent($dataInsert['content']);
                   $model->setImage($dataInsert['image']);
                   $model->setCategoryId($dataInsert['category_id']);
                   $model->setWeight($dataInsert['weight']);
                   $model->setPrice($dataInsert['price']);
                   $model->setIsBest($dataInsert['is_best']);
                   $model->setMadein($dataInsert['madein']);
                   $model->setIsSale($dataInsert['is_sale']);
                   $model->setStatus(1);
                   $model->setCreated(time());
                   $model->setNameEn($dataInsert['name_en']);
                   $model->setDescriptionEn($dataInsert['description_en']);
                   $model->setContentEn($dataInsert['content_en']);
                   $model->setNameChina($dataInsert['name_china']);
                   $model->setDescriptionChina($dataInsert['description_china']);
                   $model->setContentChina($dataInsert['content_china']);
                   $model->save();
                   $this->_redirect('/admin/products');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }
    public function editAction() {
        try {
           $model = new Vina_Model_Products();
           $form                   = new Admin_Form_Product();
           
           $id                     =  $this->_request->getParam('id',false);
           $data = $model->find($id)->toArray();
           $form->getElement('image')->setRequired(false)->setDescription("<img src='/upload/".$data['image']."' heigt = '80'/>");
           $form->populate($data);
            if ($this->_request->isPost()) {
                
                $formData = $this->_request->getPost();
                $formData = $this->_clean($formData);
                if ($form->isValid($formData)) {
                   $dataInsert = $form->getValues();
                   $model->setId($id);
                   $model->setName($dataInsert['name']);
                   $model->setDescription($dataInsert['description']);
                   $model->setContent($dataInsert['content']);
                   $model->setImage($dataInsert['image']);
                   $model->setMadein($dataInsert['madein']);
                   $model->setWeight($dataInsert['weight']);
                   $model->setCategoryId($dataInsert['category_id']);
                   $model->setPrice($dataInsert['price']);
                   $model->setIsBest($dataInsert['is_best']);
                   $model->setIsSale($dataInsert['is_sale']);
                   $model->setNameEn($dataInsert['name_en']);
                   $model->setDescriptionEn($dataInsert['description_en']);
                   $model->setContentEn($dataInsert['content_en']);
                   $model->setNameChina($dataInsert['name_china']);
                   $model->setDescriptionChina($dataInsert['description_china']);
                   $model->setContentChina($dataInsert['content_china']);
                   $model->save();
                   $this->_redirect('/admin/products');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }  
  public function deleteAction() {
     $id                     =  $this->_request->getParam('id',false);
     $product = new Vina_Model_Products();
     $where = "id = $id";
     $product->delete($where);
     $this->_redirect($_SERVER['HTTP_REFERER']);
  }
  protected function _clean($value)
    {
        return is_array($value) ? array_map(array($this, '_clean'), $value) : stripslashes($value);
    }
    	
}
